package com.oak.interviewdemo.sys.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.date.DateTime;
import com.google.common.collect.Maps;
import com.oak.interviewdemo.sys.entity.dto.StudentDto;
import com.oak.interviewdemo.utils.ExcelUtils;
import com.oak.interviewdemo.utils.ZipUtil;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: guoxy
 * @Description:
 * @Date: 2020/6/4 14:31
 */
@Controller
public class FileController {

    @GetMapping("/export1")
    public String export(HttpServletResponse response) throws IOException {
        ArrayList<StudentDto> list = new ArrayList<>();
        list.add(new StudentDto("1", "lucy","cc", 1, new Date(), new Date()));
        list.add(new StudentDto("2", "jack", "cc", 1, new Date(), new Date()));
        list.add(new StudentDto("3", "jim", "cc", 0, new Date(), new Date()));
//        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("计算机一班学生","学生"),
//                StudentDto .class, list);

        ExcelUtils.exportExcel(list, "easypoi导出功能", "导出sheet1", StudentDto.class, "测试user.xls", response);
        return "success";
    }


    /**
     * 导出
     *
     * @param response
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void exportExcel(HttpServletResponse response) throws IOException {
        long start = System.currentTimeMillis();
        List<StudentDto> personList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            StudentDto personVo = new StudentDto();
            personVo.setName("张三");
            personVo.setSex(1);
            personVo.setCtName("asd");
            personVo.setBirthday(new Date());
            personVo.setRegistrationDate(new Date());
            personList.add(personVo);
        }
        System.out.println("导出excel所花时间：" + (System.currentTimeMillis() - start));
        ExcelUtils.exportExcel(personList, "员工信息表", "员工信息", StudentDto.class, "员工信息", response);
    }

    @RequestMapping(value = "/import")
    public void importExcel(){
        ImportParams params = new ImportParams();
        //设置标题的行数，有标题时一定要有
        params.setTitleRows(1);
        //设置表头的行数
        params.setHeadRows(1);
        List<StudentDto> list = ExcelImportUtil.importExcel(new File("D:/1.xlsx"), StudentDto.class, params);
        System.out.println("解析到的数据长度是：" + list.size());

    }
    @RequestMapping(value = "/importZip")
    public void importZip(HttpServletResponse response) throws FileNotFoundException {
        List<String> fileList = new ArrayList<>();
        fileList.add("D:\\1.txt");
        fileList.add("D:\\2.txt");
        String zipUrl = "test.zip";
        ZipUtil.downloadMoreFile(response,fileList,zipUrl);
    }
}